import { Route, Switch, NavLink, Link,Redirect } from 'react-router-dom'
import {Suspense} from "react" // 路由没有加载出来前的loading
let RouterView = (props)=>{
  
    let { RouterList } = props
    let routerList = RouterList.filter((item) => {
        return !item.to
    })
    let redirect =  RouterList.filter((item) => {
        return item.to
    })[0]
    console.log(routerList,redirect);
    return (
         <Suspense fallback={<div>...loading</div>}>
             <Switch>
                {
                    routerList && routerList.length ? routerList.map((item,index)=>{
                        // 本身Route标签上是path属性和component属性，compoent只能是一个组件，
                        // render可以理解为component得升级版
                        return <Route path={item.path} render={(routerProps)=>{ // routerProps是路由信息对象
                            window.withrouter = routerProps
                            console.log(routerProps,1111);
                            if (item.children ) {
                                return  <item.component {...routerProps} child={item.children}></item.component>
                            }
                            return <item.component {...routerProps}></item.component>
                        }} key={index}></Route>
                    }):"暂无路由"
                }
                {
                    redirect?<Redirect from={redirect.from} to={redirect.to}></Redirect>:"暂无重定向"
                }
             </Switch>
         </Suspense>
    )
}
export default RouterView